package org.convallaria.system.biz.util;

import lombok.extern.slf4j.Slf4j;
import org.convallaria.system.biz.service.PasswordPolicyService;
import org.convallaria.system.biz.service.SecurityConfigService;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 安全测试工具类
 * 提供安全功能测试和验证方法
 * 
 * @author convallaria
 * @since 1.0.0
 */
@Slf4j
@Component
public class SecurityTestUtils {

    /**
     * 测试密码策略
     * 
     * @param passwordPolicyService 密码策略服务
     * @return 测试结果
     */
    public static List<PasswordTestResult> testPasswordPolicy(PasswordPolicyService passwordPolicyService) {
        List<PasswordTestResult> results = new ArrayList<>();
        
        // 测试用例
        String[] testPasswords = {
            "123456",                    // 太简单
            "password",                  // 常见密码
            "abc",                       // 太短
            "Password123",               // 缺少特殊字符
            "password123!",              // 缺少大写字母
            "PASSWORD123!",              // 缺少小写字母
            "Password!",                 // 缺少数字
            "Password123!@#",           // 符合策略
            "MySecurePass2024!",        // 符合策略
            "Test@123"                   // 符合策略
        };
        
        for (String password : testPasswords) {
            PasswordPolicyService.PasswordValidationResult result = 
                passwordPolicyService.validatePassword(password, null);
            
            results.add(new PasswordTestResult(
                password,
                result.isValid(),
                result.getMessage(),
                result.getViolation()
            ));
        }
        
        return results;
    }

    /**
     * 测试安全配置
     * 
     * @param securityConfigService 安全配置服务
     * @return 测试结果
     */
    public static SecurityConfigTestResult testSecurityConfig(SecurityConfigService securityConfigService) {
        SecurityConfigService.SecurityConfig config = securityConfigService.getSecurityConfig();
        
        return new SecurityConfigTestResult(
            config.isEnableCaptcha(),
            config.isEnableLoginLimit(),
            config.isEnablePasswordPolicy(),
            config.isEnableAuditLog(),
            config.isEnableTokenBlacklist(),
            config.isEnableDeviceFingerprint(),
            config.isEnableBehaviorAnalysis()
        );
    }

    /**
     * 生成安全报告
     * 
     * @param passwordPolicyService 密码策略服务
     * @param securityConfigService 安全配置服务
     * @return 安全报告
     */
    public static SecurityReport generateSecurityReport(
            PasswordPolicyService passwordPolicyService,
            SecurityConfigService securityConfigService) {
        
        List<PasswordTestResult> passwordResults = testPasswordPolicy(passwordPolicyService);
        SecurityConfigTestResult configResult = testSecurityConfig(securityConfigService);
        
        int totalPasswordTests = passwordResults.size();
        int passedPasswordTests = (int) passwordResults.stream().filter(PasswordTestResult::isValid).count();
        
        return new SecurityReport(
            totalPasswordTests,
            passedPasswordTests,
            configResult,
            passwordResults
        );
    }

    /**
     * 密码测试结果
     */
    public static class PasswordTestResult {
        private final String password;
        private final boolean valid;
        private final String message;
        private final PasswordPolicyService.PasswordPolicyViolation violation;

        public PasswordTestResult(String password, boolean valid, String message, 
                                PasswordPolicyService.PasswordPolicyViolation violation) {
            this.password = password;
            this.valid = valid;
            this.message = message;
            this.violation = violation;
        }

        public String getPassword() { return password; }
        public boolean isValid() { return valid; }
        public String getMessage() { return message; }
        public PasswordPolicyService.PasswordPolicyViolation getViolation() { return violation; }
    }

    /**
     * 安全配置测试结果
     */
    public static class SecurityConfigTestResult {
        private final boolean captchaEnabled;
        private final boolean loginLimitEnabled;
        private final boolean passwordPolicyEnabled;
        private final boolean auditLogEnabled;
        private final boolean tokenBlacklistEnabled;
        private final boolean deviceFingerprintEnabled;
        private final boolean behaviorAnalysisEnabled;

        public SecurityConfigTestResult(boolean captchaEnabled, boolean loginLimitEnabled,
                                      boolean passwordPolicyEnabled, boolean auditLogEnabled,
                                      boolean tokenBlacklistEnabled, boolean deviceFingerprintEnabled,
                                      boolean behaviorAnalysisEnabled) {
            this.captchaEnabled = captchaEnabled;
            this.loginLimitEnabled = loginLimitEnabled;
            this.passwordPolicyEnabled = passwordPolicyEnabled;
            this.auditLogEnabled = auditLogEnabled;
            this.tokenBlacklistEnabled = tokenBlacklistEnabled;
            this.deviceFingerprintEnabled = deviceFingerprintEnabled;
            this.behaviorAnalysisEnabled = behaviorAnalysisEnabled;
        }

        // Getters
        public boolean isCaptchaEnabled() { return captchaEnabled; }
        public boolean isLoginLimitEnabled() { return loginLimitEnabled; }
        public boolean isPasswordPolicyEnabled() { return passwordPolicyEnabled; }
        public boolean isAuditLogEnabled() { return auditLogEnabled; }
        public boolean isTokenBlacklistEnabled() { return tokenBlacklistEnabled; }
        public boolean isDeviceFingerprintEnabled() { return deviceFingerprintEnabled; }
        public boolean isBehaviorAnalysisEnabled() { return behaviorAnalysisEnabled; }
    }

    /**
     * 安全报告
     */
    public static class SecurityReport {
        private final int totalPasswordTests;
        private final int passedPasswordTests;
        private final SecurityConfigTestResult configResult;
        private final List<PasswordTestResult> passwordResults;

        public SecurityReport(int totalPasswordTests, int passedPasswordTests,
                            SecurityConfigTestResult configResult,
                            List<PasswordTestResult> passwordResults) {
            this.totalPasswordTests = totalPasswordTests;
            this.passedPasswordTests = passedPasswordTests;
            this.configResult = configResult;
            this.passwordResults = passwordResults;
        }

        // Getters
        public int getTotalPasswordTests() { return totalPasswordTests; }
        public int getPassedPasswordTests() { return passedPasswordTests; }
        public SecurityConfigTestResult getConfigResult() { return configResult; }
        public List<PasswordTestResult> getPasswordResults() { return passwordResults; }
        
        public double getPasswordTestPassRate() {
            return totalPasswordTests > 0 ? (double) passedPasswordTests / totalPasswordTests : 0.0;
        }
    }
}
